/* 
 * Copyright 2014 Josef Hertl.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package caetus;

import caetus.pkcs11.session.CommandLineOptions;
import caetus.pkcs11.session.Session;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Josef Hertl
 */
public class Caetus {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CommandLineOptions options = new CommandLineOptions(args);
        if (options.getCheckFilePath()) {
            performModelChecking();
        } else {
            TokenInitializer initializer = new TokenInitializer(options);
            initializer.initialize();
            if (options.isPerformModelChecking()) {
                performModelChecking();
            }
        }
    }

    /**
     * Prepares a model for the model checker, passes it over and performs the
     * checking.
     */
    private static void performModelChecking() {
        System.out.println("\n\n\n\n");
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"bash", "-c", ". testModelSATMC"});
            p.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException ex) {
//            ex.printStackTrace();
            System.err.println("Model checking failed because the file was not found. " + ex.getMessage());
            Logger.getLogger(Session.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
//            ex.printStackTrace();
            System.err.println("Model checking failed: " + ex.getMessage());
            Logger.getLogger(Session.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
//            ex.printStackTrace();
            System.err.println("Model checking failed: " + ex.getMessage());
            Logger.getLogger(Session.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
